package com.tokencloud.consumer.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
// 当这个enable属性值为true时才生效
//@ConditionalOnProperty(prefix = "spring.rabbitmq.listener.simple.retry", name = "enable", havingValue = "true")
public class ErrorConfiguration {


    private static final Logger log = LoggerFactory.getLogger(ErrorConfiguration.class);

    @Bean
    public DirectExchange errorDirectExchange() {

        return new DirectExchange("error.direct");
    }

    @Bean
    public Queue errorQueue() {
        return new Queue("error.queue");
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(errorQueue()).to(errorDirectExchange()).with("error");
    }

    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate){
//        log.error(">>>>>>>>>>> 存储到 error.direct 交换机， error队列");
        return new RepublishMessageRecoverer(rabbitTemplate, "error.direct", "error" );
    }



}
